﻿<div class="status">
    <label>@StatusText</label>
</div>

@code {

    [Parameter] public int DisplayIntervalSecs { get; set; } = 5;

    [Parameter] public string StatusText { get; set; }

    private Timer? searchTimer;

    protected override async Task OnParametersSetAsync()
    {
        SetStatusText(StatusText);

        await base.OnParametersSetAsync();
    }

    private void SetStatusText(string value)
    {
        KillTimer();

        if( ! string.IsNullOrEmpty( value ) )
            searchTimer = new Timer(TimerCallback, null, DisplayIntervalSecs * 1000, Timeout.Infinite);

        StateHasChanged();
    }

    private void KillTimer()
    {
        var oldTimer = searchTimer;
        searchTimer = null;
        if( oldTimer != null )
            oldTimer.Dispose();
    }

    private void TimerCallback(object? state)
    {
        SetStatusText(string.Empty);
    }

}